package model.grove;

import framework.common.SingleSensor;

/**
 * On/Offの二つの状態をもつ単純なセンサの抽象クラス
 * @author uehara
 *
 */
abstract public class OnOffSensor extends SingleSensor {
	public static final Integer OFF = Integer.valueOf(0); // off
	public static final Integer ON = Integer.valueOf(1); // on

	public String attributeName;

	protected OnOffSensor(String attributeName) {
		this.attributeName = attributeName;
	}

	public Object getAttribute(String attr) {
		if (attr.equals(attributeName)) {
			Object value = super.getAttribute(attr);
			// OFF if null
			if (value == null)
				return OFF;
			return value;
		}
		throw new RuntimeException("Illegal Attribute: " + attr);
	}

	public void setAttribute(String attr, Object value) {
		if (attr.equals(attributeName)) {
			if (value == ON || value == OFF) {
				super.setAttribute(attr, value);
				return;
			}
			throw new RuntimeException("Illegal Value");
		}
		throw new RuntimeException("Illegal Attribute: " + attr);
	}

	public Object getAttribute() {
		return getAttribute(attributeName);
	}

	public void setAttribute(Object value) {
		setAttribute(attributeName, value);
	}

	public boolean isOn() {
		return getAttribute() == ON;
	}

	public boolean isOff() {
		return ! isOn();
	}

	public void on() {
		setAttribute(ON);
	}

	public void off() {
		setAttribute(OFF);
	}

	public void turn(boolean onOff) {
		if (onOff)
			on();
		else
			off();
	}

	public void turn() {
		if (isOn())
			off();
		else
			on();
	}
}
